package com.infosys.cabplanning.service.driver;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.infosys.cabplanning.common.domain.Driver;
import com.infosys.cabplanning.helper.JsonFormatter;
import com.infosys.cabplanningdao.CabPlanningDAOService;
import com.infosys.cabplanningdao.driver.dao.DriverDAO;

@Controller
public class DriverController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @Autowired
    public CabPlanningDAOService daoService;
    
    @Autowired
    JsonFormatter jsonFormatter;
    
    @RequestMapping(value="driver", method = RequestMethod.POST,produces="application/json",consumes="application/json")
    public @ResponseBody List<Driver> driver(@RequestBody Driver driver) throws Exception {
    		System.out.println("Entered Drivers method");
    		System.out.println(driver.getDriverId() + driver.getFirstName() + driver.getDrivingLicenseNumber());
    		List<Driver> driverList = daoService.findDriver(driver);
    		return driverList; 
    }
    
    @RequestMapping(value="savedriver", method = RequestMethod.POST ,produces="application/json",consumes="application/json")
    public @ResponseBody List<Driver> savedriver(@RequestBody String json) throws Exception {
    		System.out.println("Entered Drivers method");
    		
    		List<Object> list = jsonFormatter.convertToList(json, new Driver());
    		List<Driver> driverList = new ArrayList<Driver>();
    		for(Object obj : list) {
    			driverList.add((Driver)obj);
    		}
    		daoService.saveDriver(driverList);
    		
		 return driverList; 
    }
}